<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :filter-node-method="filterNode"
            :expand-on-click-node="false"
            ref="tree"
            node-key="value"
            :default-expand-all="false"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="三级分类" prop="cthrId">
            <el-input
              v-model="queryParams.cthrId"
              placeholder="请输入三级分类"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="品牌" prop="brdId">
            <el-input
              v-model="queryParams.brdId"
              placeholder="请输入品牌"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品名称" prop="goodsName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入商品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品代号" prop="goodsCode">
            <el-input
              v-model="queryParams.goodsCode"
              placeholder="请输入商品代号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['webshop:GoodsSpuInfo:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['webshop:GoodsSpuInfo:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['webshop:GoodsSpuInfo:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['webshop:GoodsSpuInfo:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="GoodsSpuInfoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="商品主键" align="center" prop="goodsId"/>
          <el-table-column label="一级分类" align="center" prop="coneName"/>
          <el-table-column label="二级分类" align="center" prop="ctwoName"/>
          <el-table-column label="三级分类" align="center" prop="cthrName"/>
          <el-table-column label="品牌" align="center" prop="brandCnName"/>
          <el-table-column label="商品名称" align="center" prop="goodsName"/>
          <el-table-column label="商品代号" align="center" prop="goodsCode"/>
<!--          <el-table-column label="详细说明" align="center" prop="goodsInfo"/>-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['webshop:GoodsSpuInfo:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['webshop:GoodsSpuInfo:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />




      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {
    listGoodsSpuInfo,
    getGoodsSpuInfo,
    delGoodsSpuInfo,
    addGoodsSpuInfo,
    updateGoodsSpuInfo
  } from "@/api/webshop/GoodsSpuInfo";
  import {getCascader } from "@/api/webshop/CategoryThree";
  export default {
    name: "CategoryTree",
    data() {
      return {
        categoryName:"",
        categoryOptions:[],
        defaultProps: {
          children: "children",
          label: "label"
        },

        //----------------------------------------
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品表格数据
        GoodsSpuInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          coneId: null,
          ctwoId: null,
          cthrId: null,
          brdId: null,
          goodsName: null,
          goodsCode: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},

        oneList: [],
        twoList: [],
        threeList: [],
        brandList: [],
        brandFullList:[],

        value: [],
        options: []

      }
    },

    watch: {
      // 根据名称筛选部门树
      categoryName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getCascader()
      this.getList();

    },
    methods: {
      getCascader(){
        getCascader().then(resp=>{
          this.categoryOptions = resp.data
        })
      },
      filterNode(value, data, node) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1
      },

      handleNodeClick(data,  node, self) {
        // console.log( data,  node, self )
        console.log(node.level,  data.value);
        if(node.level == 1){
          this.queryParams.coneId = data.value
        }
        if(node.level == 2){
          this.queryParams.ctwoId = data.value
        }
        if(node.level == 3) {
          this.queryParams.cthrId = data.value
        }
        this.handleQuery();
      },

      //---------------------------------------
      /** 查询商品列表 */
      getList() {
        this.loading = true;
        listGoodsSpuInfo(this.queryParams).then(response => {
          this.GoodsSpuInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          goodsId: null,
          coneId: null,
          ctwoId: null,
          cthrId: null,
          brdId: null,
          goodsName: null,
          goodsCode: null,
          goodsInfo: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.goodsId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const goodsId = row.goodsId || this.ids
        getGoodsSpuInfo(goodsId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商品";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.goodsId != null) {
              updateGoodsSpuInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGoodsSpuInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const goodsIds = row.goodsId || this.ids;
        this.$modal.confirm('是否确认删除商品编号为"' + goodsIds + '"的数据项？').then(function () {
          return delGoodsSpuInfo(goodsIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('webshop/GoodsSpuInfo/export', {
          ...this.queryParams
        }, `GoodsSpuInfo_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>


<style scoped lang="scss">

</style>
